<template>
    <Header></Header>

    <div class="container mx-auto max-w-screen-xl mt-5">
        <div class="grid grid-cols-4">
            <!-- 左边栏 -->
            <div class="col-span-4 px-3 md:col-span-3 sm:col-span-4">
                <div class="mb-3 p-5 bg-white border border-gray-200 rounded-lg shadow-sm dark:bg-gray-800 dark:border-gray-700">
                    <h2 class="mb-6 font-bold text-gray-900 text-2xl dark:text-white">文章标签</h2>
                    <p class="mb-6 text-gray-600 dark:text-gray-400">通过标签浏览相关技术文章，快速找到您感兴趣的内容。</p>

                    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
                        <div @click="goTagArticleListPage(item.id, item.name)" v-for="(item, index) in tags" :key="index"
                            class="p-4 bg-white border border-gray-200 rounded-lg shadow-sm hover:shadow-md transition-all duration-300 cursor-pointer hover:-translate-y-1">
                            <div class="flex items-center mb-2">
                                <div class="flex-shrink-0 w-8 h-8 mr-3 flex items-center justify-center rounded-full" 
                                     :class="[
                                        index % 9 === 0 ? 'bg-blue-100 text-blue-500' : 
                                        index % 9 === 1 ? 'bg-green-100 text-green-500' : 
                                        index % 9 === 2 ? 'bg-purple-100 text-purple-500' : 
                                        index % 9 === 3 ? 'bg-yellow-100 text-yellow-500' : 
                                        index % 9 === 4 ? 'bg-red-100 text-red-500' : 
                                        index % 9 === 5 ? 'bg-indigo-100 text-indigo-500' : 
                                        index % 9 === 6 ? 'bg-pink-100 text-pink-500' : 
                                        index % 9 === 7 ? 'bg-teal-100 text-teal-500' : 
                                        'bg-orange-100 text-orange-500'
                                     ]">
                                    <svg class="w-5 h-5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
                                        <path stroke-linecap="round" stroke-linejoin="round" d="M9.568 3H5.25A2.25 2.25 0 003 5.25v4.318c0 .597.237 1.17.659 1.591l9.581 9.581c.699.699 1.78.872 2.607.33a18.095 18.095 0 005.223-5.223c.542-.827.369-1.908-.33-2.607L11.16 3.66A2.25 2.25 0 009.568 3z" />
                                        <path stroke-linecap="round" stroke-linejoin="round" d="M6 6h.008v.008H6V6z" />
                                    </svg>
                                </div>
                                <h3 class="text-lg font-bold text-gray-800 hover:text-blue-600 transition-colors duration-300">
                                    {{ item.name }}
                                </h3>
                            </div>
                            <p class="text-gray-600 text-sm mb-3">{{item.description || `相关文章: ${item.articleCount || 0}篇`}}</p>
                            <div class="flex justify-between items-center">
                                <span class="bg-gray-100 text-gray-800 text-xs font-medium px-2.5 py-0.5 rounded-full">
                                    {{item.articleCount || 0}} 篇文章
                                </span>
                                <button class="text-sm font-medium text-blue-600 hover:underline flex items-center">
                                    查看文章
                                    <svg class="w-3.5 h-3.5 ml-1" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 10">
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M1 5h12m0 0L9 1m4 4L9 9"/>
                                    </svg>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- 右边栏 -->
            <div class="col-span-4 px-3 md:col-span-1 sm:col-span-4">
                <div class="sticky top-21 space-y-4">
                    <UserInfoCard></UserInfoCard>
                    
                    <!-- 热门分类 -->
                    <div class="mb-3 w-full font-medium p-5 bg-white border border-gray-200 rounded-lg shadow-sm dark:bg-gray-800 dark:border-gray-700">
                        <h4 class="mb-3 font-bold text-gray-900 dark:text-white">热门分类</h4>
                        <div class="flex flex-wrap gap-2">
                            <div @click="goCategoryArticleListPage(item.id, item.name)" v-for="(item, index) in categories" :key="index"
                                class="px-3 py-1.5 bg-blue-50 text-blue-600 text-xs font-medium rounded-full hover:bg-blue-100 hover:text-blue-700 transition-colors duration-200 dark:hover:bg-blue-800 dark:hover:text-blue-300 dark:bg-blue-900 dark:text-blue-300">
                                {{ item.name }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <Footer></Footer>
</template>

<script setup>
import Header from '@/layouts/components/Header.vue'
import Footer from '@/layouts/components/Footer.vue'
import UserInfoCard from '@/components/UserInfoCard.vue'
import { useRouter } from 'vue-router'
import { getTags } from '@/api/frontend/tag'
import { getCategories } from '@/api/frontend/category'
import { ref } from 'vue'

const router = useRouter()

const goTagArticleListPage = (id, name) => {
    router.push({path: '/tag/list', query: {id: id, name: name}})
}

const goCategoryArticleListPage = (id, name) => {
    router.push({ path: '/category/list', query: { id: id, name: name } })
}

const tags = ref([])
getTags().then((e) => {
    if (e.success) {
        tags.value = e.data
        console.log('获取到标签数据:', tags.value)
    }
})

const categories = ref([])
getCategories().then((e) => {
    if (e.success) {
        categories.value = e.data
        console.log('获取到分类数据:', categories.value)
    }
})
</script>